package com.shelpe.services.sinbad.entity.jpa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.controller.LogDto.LogNoteable;
import com.shelpe.services.sinbad.service.utils.EntityUtils.Boolean2StringConverter;
import com.shelpe.services.sinbad.service.utils.EntityUtils.DriveTypeConverter;
import com.shelpe.services.sinbad.service.utils.EntityUtils.OnlineStatusConverter;
import com.shelpe.services.sinbad.utils.CampaignUtils;

import lombok.Data;

@Entity(name = "campaigns")
@Data
public class Campaign implements LogNoteable {

	@Id
	@Column(name = "campaign_id")
	@JsonProperty("campaign_id")
	private long autoID;
	@Column(name = "user_id")
	@JsonProperty("user_id")
	private long userID;
	private String nick;
	private String title;
	@JsonIgnore
	private int budget;
	@Convert(converter = Boolean2StringConverter.class)
	@JsonIgnore
	private boolean useSmooth;
	@JsonIgnore
	private String incrementalPrice;
	@JsonIgnore
	private String platform;
	@Convert(converter = DriveTypeConverter.class)
	@JsonIgnore
	private DriveType autoType = DriveType.manual;
	@Column(name = "`limit`")
	@JsonIgnore
	private int limit;
	@JsonProperty("online_status")
	@Convert(converter = OnlineStatusConverter.class)
	private OnlineStatus onlineStatus;
	@JsonProperty("settle_status")
	@Convert(converter = OnlineStatusConverter.class)
	private OnlineStatus settleStatus;
	@JsonProperty("settle_reason")
	private String settleReason;

	@JsonProperty("create_time")
	private Date createTime;
	@JsonProperty("modified_time")
	private Date modifiedTime;
	@JsonProperty("updated_time")
	private Date updatedTime;
	@JsonIgnore
	private String schedule;
	@Convert(converter = AreaConverter.class)
	@JsonIgnore
	private Area area;
	@Convert(converter = DriveTypeConverter.class)
	private DriveType previousAutoType; // 上一次的推广模式
	@JsonProperty("auto_type_change_time")
	@Column(name = "auto_type_change_time")
	private Date changeTime;// 模式切换时间
	@Column(name = "campaign_limit")
	private int campaignLimit;

	@Data
	public static class Area {
		private boolean asAll;
		private List<Integer> nums;

		public void addArea(int num) {
			if (this.nums == null) {
				this.nums = new ArrayList<Integer>();
			}
			this.nums.add(num);
		}
	}

	@Data
	public static class RangeObject {
		@JsonProperty("day_of_week")
		private int dayOfWeek;
		@JsonFormat(shape = Shape.STRING, pattern = "HH:mm")
		private String from;
		@JsonFormat(shape = Shape.STRING, pattern = "HH:mm")
		private String to;
		private int discount;
	}

	@Data
	public static class ScheduleObject {
		private boolean asAll;
		private List<RangeObject> ranges;

		public void addRange(RangeObject range) {
			if (this.ranges == null) {
				this.ranges = new ArrayList<RangeObject>();
			}
			this.ranges.add(range);
		}
	}

	public static class AreaConverter implements AttributeConverter<Area, String> {

		@Override
		public String convertToDatabaseColumn(Area attribute) {
			return CampaignUtils.fromArea(attribute);
		}

		@Override
		public Area convertToEntityAttribute(String dbData) {
			return CampaignUtils.toArea(dbData);
		}
	}

	public static class ScheduleConverter implements AttributeConverter<ScheduleObject, String> {

		@Override
		public String convertToDatabaseColumn(ScheduleObject attribute) {
			return CampaignUtils.fromScheduleObject(attribute);
		}

		@Override
		public ScheduleObject convertToEntityAttribute(String dbData) {
			return CampaignUtils.toScheduleObject(dbData);
		}
	}
}
